﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BillingServiceRef;

    public class InvoiceDetail : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected Button btnSaveContinue;
        protected DropDownList ddlStatus;
        protected HtmlForm F9213;
        protected HtmlInputHidden hdnBillingObject;
        protected HtmlInputHidden hdnBillingObjectID;
        protected HtmlInputHidden hdnDurFromTo;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnInvoiceID;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnPayAmount;
        protected HtmlHead Head1;
        protected HtmlImage imgStatementID;
        protected Label lblAvailAmount;
        protected Label lblInvoiceAmount;
        protected Label lblInvoiceDetail;
        protected Label lblInvoiceType;
        protected Label lblLineID;
        protected Label lblMsg;
        protected Label lblPayMethod;
        protected Label lblRemark;
        protected Label lblStatementID;
        protected Label lblStatus;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAvailAmount;
        protected TextBox txtInvoiceAmount;
        protected TextBox txtInvoiceType;
        protected TextBox txtLineID;
        protected TextBox txtPayMethod;
        protected TextBox txtRemark;
        protected TextBox txtStatementID;
        protected UpdatePanel up1;

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("Invoice.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.hdnInvoiceID.Value)));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.ddlStatus.SelectedValue != "New")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9210_021");
            }
            else if (string.IsNullOrEmpty(this.txtStatementID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9210_019");
            }
            else
            {
                decimal tempDec = 0M;
                if (!string.IsNullOrEmpty(this.txtInvoiceAmount.Text.Trim()) && decimal.TryParse(this.txtInvoiceAmount.Text.Trim(), out tempDec))
                {
                    tempDec = decimal.Parse(this.txtInvoiceAmount.Text.Trim());
                }
                if (tempDec <= 0M)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9210_022");
                }
                else if (tempDec > decimal.Parse(this.txtAvailAmount.Text))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9210_023");
                }
                else if (this.InserInvoiceDetail())
                {
                    base.Server.Transfer(string.Format("Invoice.aspx?from={0}&detail={1}&ID={2}", base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.hdnInvoiceID.Value)));
                }
            }
        }

        protected void btnSaveContinue_Click(object sender, EventArgs e)
        {
            if (this.ddlStatus.SelectedValue != "New")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9210_021");
            }
            else if (string.IsNullOrEmpty(this.txtStatementID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9210_019");
            }
            else
            {
                decimal tempDec = 0M;
                if (!string.IsNullOrEmpty(this.txtInvoiceAmount.Text.Trim()) && decimal.TryParse(this.txtInvoiceAmount.Text.Trim(), out tempDec))
                {
                    tempDec = decimal.Parse(this.txtInvoiceAmount.Text.Trim());
                }
                if (tempDec <= 0M)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9210_022");
                }
                else if (tempDec > decimal.Parse(this.txtAvailAmount.Text))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9210_023");
                }
                else if (this.InserInvoiceDetail())
                {
                    this.txtLineID.Text = (decimal.Parse(this.hdnLineID.Value) + 10M).ToString();
                    this.txtStatementID.Text = "";
                    this.txtPayMethod.Text = "";
                    this.txtRemark.Text = "";
                    this.txtInvoiceAmount.Text = "";
                    this.txtInvoiceType.Text = "";
                    this.ddlStatus.SelectedValue = "New";
                    this.hdnPayAmount.Value = "0";
                    this.txtAvailAmount.Text = "0";
                    this.txtStatementID.Enabled = true;
                    this.txtInvoiceAmount.Enabled = true;
                    this.hdnLineID.Value = this.txtLineID.Text;
                }
            }
        }

        private bool InserInvoiceDetail()
        {
            bool result = false;
            try
            {
                InvoiceDetailInfo invoiceDetailInfo = new InvoiceDetailInfo {
                    InvoiceID = this.hdnInvoiceID.Value,
                    LineID = Convert.ToInt32(this.hdnLineID.Value),
                    StatementID = this.txtStatementID.Text.Trim(),
                    PayMethod = this.txtPayMethod.Text.Trim()
                };
                if (string.IsNullOrEmpty(this.hdnPayAmount.Value))
                {
                    this.hdnPayAmount.Value = "0";
                }
                invoiceDetailInfo.PayAmount = decimal.Parse(this.hdnPayAmount.Value);
                invoiceDetailInfo.Remark = this.txtRemark.Text.Trim();
                invoiceDetailInfo.Descr = this.txtInvoiceType.Text.Trim();
                invoiceDetailInfo.Status = this.ddlStatus.SelectedValue;
                invoiceDetailInfo.CreatedBy = base.UserInfo.UserID;
                invoiceDetailInfo.UpdatedBy = base.UserInfo.UserID;
                invoiceDetailInfo.InvoiceAmount = decimal.Parse(this.txtInvoiceAmount.Text.Trim());
                if (string.IsNullOrEmpty(BillingServiceClientProxy.GetInstance().InsertInvoiceDetail(invoiceDetailInfo)))
                {
                    this.lblMsg.Text = this.hdnLineID.Value + base.GetGlobalMsgResource("F9210_010");
                }
                else
                {
                    result = true;
                    this.lblMsg.Text = this.hdnLineID.Value + base.GetGlobalMsgResource("F9210_009");
                }
                this.hdnFlag.Value = "1";
                return result;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                return result;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlStatus, "InvoiceDetailStatus");
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                string from = base.Request.QueryString["from"];
                this.hdnFrom.Value = from;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnInvoiceID.Value = base.Request.QueryString["ID"];
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["LineID"]))
            {
                this.hdnLineID.Value = base.Request.QueryString["LineID"];
                this.txtLineID.Text = this.hdnLineID.Value;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["BillingObject"]))
            {
                this.hdnBillingObject.Value = base.Request.QueryString["BillingObject"];
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["BillingObjectID"]))
            {
                this.hdnBillingObjectID.Value = base.Request.QueryString["BillingObjectID"];
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["DueFromTo"]))
            {
                this.hdnDurFromTo.Value = base.Request.QueryString["DueFromTo"];
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["Type"]))
            {
                this.SetInvoiceDetail();
                this.hdnFlag.Value = "1";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetInvoiceDetail()
        {
            InvoiceDetailInfo invoiceDetailInfo = BillingServiceClientProxy.GetInstance().GetInvoiceDetailInfoByID(this.hdnInvoiceID.Value, this.hdnLineID.Value);
            if (invoiceDetailInfo != null)
            {
                this.txtLineID.Text = this.hdnLineID.Value;
                this.txtStatementID.Text = invoiceDetailInfo.StatementID;
                this.txtPayMethod.Text = invoiceDetailInfo.PayMethod;
                this.txtRemark.Text = invoiceDetailInfo.Remark;
                this.txtInvoiceAmount.Text = invoiceDetailInfo.InvoiceAmount.ToString();
                this.txtInvoiceType.Text = invoiceDetailInfo.Descr;
                this.ddlStatus.SelectedValue = invoiceDetailInfo.Status;
                this.hdnPayAmount.Value = invoiceDetailInfo.PayAmount.ToString();
                this.txtStatementID.Enabled = false;
                this.txtInvoiceAmount.Enabled = false;
                this.btnSaveContinue.Enabled = false;
                StatementInfo statementInfo = BillingServiceClientProxy.GetInstance().GetStatementByID(this.txtStatementID.Text);
                if (statementInfo != null)
                {
                    this.txtAvailAmount.Text = ((statementInfo.TotalAmount - statementInfo.InvoiceAmount) + invoiceDetailInfo.InvoiceAmount).ToString();
                }
            }
        }

        protected void txtStatementID_TextChanged(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            this.txtAvailAmount.Text = "0";
            if (!string.IsNullOrEmpty(this.txtStatementID.Text.Trim()))
            {
                StatementInfo statementInfo = BillingServiceClientProxy.GetInstance().GetStatementByID(this.txtStatementID.Text);
                if (statementInfo != null)
                {
                    if ((statementInfo.Status == "Reviewed") || (statementInfo.Status == "Processing"))
                    {
                        if (((statementInfo.BillingObject.ToUpper() == this.hdnBillingObject.Value.ToUpper()) && (statementInfo.BillingObjectID.ToUpper() == this.hdnBillingObjectID.Value.ToUpper())) && (statementInfo.DueFromTo.ToUpper() == this.hdnDurFromTo.Value.ToUpper()))
                        {
                            this.txtAvailAmount.Text = (statementInfo.TotalAmount - statementInfo.InvoiceAmount).ToString();
                        }
                        else
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F9210_020");
                        }
                    }
                    else
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F9210_027");
                    }
                }
            }
        }
    }
}

